﻿/***********************************************************************************//*
*  作者: 邹杭特                                                                       *
*  时间: 2021-04-24                                                                   *
*  版本: master_b9fdd5                                                                *
*  功能:                                                                              *
*   - 子界面绑定                                                                      *
*//************************************************************************************/

using System.Collections.Generic;

namespace BridgeUI.Binding
{
    [BridgeUI.Attributes.PanelParent]
    public abstract class BindingSubPanel : BridgeUI.IUIControl
    {
        private IUIPanel context;
        private bool initialized;

        public IUIPanel Context { get { return context; } }
        public bool Initialized { get { return initialized; } }

        private List<IUIControl> subControls;

        public void Initialize(IUIPanel context = null)
        {
            if (!initialized)
            {
                this.context = context;
                initialized = true;
                if (subControls != null)
                {
                    using (var enumeraotr = subControls.GetEnumerator())
                    {
                        while (enumeraotr.MoveNext())
                        {
                            enumeraotr.Current.Initialize(Context);
                        }
                    }
                }
                OnInitialize();
               
            }
        }
        public void Recover()
        {
            if (initialized)
            {
                if (subControls != null)
                {
                    using (var enumeraotr = subControls.GetEnumerator())
                    {
                        while (enumeraotr.MoveNext())
                        {
                            enumeraotr.Current.Recover();
                        }
                    }
                }
                OnRecover();
                initialized = false;
            }
        }
        public abstract void OnInitialize();
        public abstract void OnRecover();
        public abstract void Binding(BindingReference target);
        public abstract void UnBinding();

        /// <summary>
        /// 绑定子面板
        /// </summary>
        protected T BindingSubReference<T>(BridgeUI.Binding.BindingReference subReference) where T : BridgeUI.Binding.BindingSubPanel, new()
        {
            var subPanel = new T();
            subPanel.Binding(subReference);
            RegistUIControl(subPanel);
            return subPanel;
        }

        /// <summary>
        /// 解绑定子面板
        /// </summary>
        /// <param name="subPanel"></param>
        protected void UnBindingSubReference(BindingSubPanel subPanel)
        {
            if(subPanel!= null)
            {
                subPanel.UnBinding();
                RemoveUIControl(subPanel);
            }
        }

        protected void RegistUIControl(IUIControl subPanel) 
        {
            if (subPanel == null) return;

            if (subControls == null)
                subControls = new List<IUIControl>();

            if(!subControls.Contains(subPanel))
            {
                subControls.Add(subPanel);
            }
        }

        protected void RemoveUIControl(IUIControl subPanel)
        {
            if (subPanel == null) return;

            if (subControls.Contains(subPanel))
            {
                subControls.Remove(subPanel);
            }
        }
    }
}